package com.example.waimaiandroid;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.waimai.MainActivity;
import com.example.waimai.pojo.Food;
import com.example.waimai.ui.home.HomeFragment;

import java.util.List;

public class GoodsCartAdapter extends BaseAdapter {

    private Context context;
    private List<Food> list;
    private LayoutInflater mInflater;
    private int num;
    private GoodsCartAdapterCallback mCallBack;
    public GoodsCartAdapter(Context context,List<Food> list,GoodsCartAdapterCallback callBack){
        this.context = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
        mCallBack = callBack;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // 更新数据
    public void update(int position,Food model){
        list.set(position,model);
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder ;
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.listitem_goods_cart,null);
            holder = new ViewHolder();
            holder.mName = (TextView) convertView.findViewById(R.id.tv_name_cart);
            holder.mNum = (TextView) convertView.findViewById(R.id.tv_num_cart);
            holder.mPrice = (TextView)convertView.findViewById(R.id.price_cart);
            holder.mAdd = (ImageView)convertView.findViewById(R.id.add_cart);
            holder.mReduce = (ImageView) convertView.findViewById(R.id.reduce_cart);
            holder.mFoodImage = (ImageView) convertView.findViewById(R.id.foodImage);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        //初始化
        holder.mName.setText(list.get(position).getFoodName());
        holder.mPrice.setText("￥" + list.get(position).getFoodPrice());
        holder.mNum.setText(list.get(position).getAddNums() + "");
        Glide.with(convertView).load(list.get(position).getFoodImage()).into(holder.mFoodImage);

        holder.mAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.onClickItem(position,true);
            }
        });

        holder.mReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.onClickItem(position,false);
            }
        });

//
//        double totalPrice = num * Double.parseDouble(list.get(position).getProdPrice());
//        list.get(position).setProdTotal(totalPrice);

/*        holder.mAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = list.get(position).getProdCount();
                count ++;
                list.get(position).setProdCount(count);
                context.modifyGoodsNum(list.get(position).getProdId(),count);
                context.calculateTotalPrice();
                notifyDataSetChanged();
                context.updateList(true,true,false);
            }
        });
        holder.mReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = list.get(position).getProdCount();
                count--;
                if (count <= 0)
                    count = 0;
                list.get(position).setProdCount(count);
                context.modifyGoodsNum(list.get(position).getProdId(),count);
                context.calculateTotalPrice();
                notifyDataSetChanged();
                context.updateList(true,true,false);
            }
        });*/
        return convertView;
    }


    class ViewHolder{
        private TextView mName;
        private TextView mPrice;
        private TextView mNum;
        private ImageView mAdd;
        private ImageView mReduce;
        private ImageView mFoodImage;
    }
}
